package Dao;

import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import entity.Record;
import entity.Top;
import entity.User;
import util.Dbconnect;
 
 
public class RecordDaoimpl implements RecordDao {

	Calendar cal = Calendar.getInstance();
	int today = cal.get(Calendar.DAY_OF_MONTH);
	int tomonth = cal.get(Calendar.MONTH) + 1;
	int toyear = cal.get(Calendar.YEAR);

	@Override
	public List<Record> getpass(String email,Integer begin,Integer limit) throws Exception {
		// TODO Auto-generated method stub
		String sql = "select * from Record where email=?and flag=2";
		if(limit != null) {
			sql = sql + " limit "+begin+","+limit+" ";
		}
		PreparedStatement st = Dbconnect.init(sql);
		st.setString(1, email);
		List<Record> query = Dbconnect.query(st, Record.class);
		return query;
	}

	@Override//获取某人背过单词记录
	public List<Record> getseen(String email,Integer begin,Integer limit) throws Exception {
		// TODO Auto-generated method stub
		String sql = "select * from Record where email=?and flag=1";
		if(limit != null) {
			sql = sql + " limit "+begin+","+limit+" ";
		}
		PreparedStatement st = Dbconnect.init(sql);
		st.setString(1, email);
		List<Record> query = Dbconnect.query(st, Record.class);
		return query;

	}

	@Override//添加背单词记录
	public Boolean addrecord(List<Record> rs) throws Exception {
		String sql;
		int update = 0;
		for (Record rd : rs) {
			sql = "insert into record(email,wordID,count,flag,year,month,day) values(?,?,?,?,?,?,?)";
			PreparedStatement st1 = Dbconnect.init(sql);
			st1.setString(1, rd.getEmail());
			st1.setInt(2, rd.getWordID());
			st1.setInt(3, rd.getCount());
			st1.setInt(4, rd.getFlag());
			st1.setInt(5, rd.getYear());
			st1.setInt(6, rd.getMonth());
			st1.setInt(7, rd.getDay());
			update += Dbconnect.update(st1);
			Dbconnect.close(st1);
		}

		return update > 0;
	}

	@Override//获取某人今天背诵单词的记录
	public List<Record> todaywords(String email) throws Exception {

		String sql = "select * from Record where email=? and day=? ";
		PreparedStatement st = Dbconnect.init(sql);
		st.setString(1, email);
		st.setInt(2, today);
		List<Record> query = Dbconnect.query(st, Record.class);
		return query;

	}

	@Override
	public List<Record> beforewords(String email,Integer begin,Integer limit) throws Exception {


		String sql = "select * from Record where email=?and day<>?";
		if(limit != null) {
			sql = sql + " limit "+begin+","+limit+" ";
		}
		PreparedStatement st = Dbconnect.init(sql);
		st.setString(1, email);
		st.setInt(2, today);
		List<Record> query = Dbconnect.query(st, Record.class);
		return query;
	}
	public static void main(String[] args) throws Exception {
		String email = "2263130346@qq.com";
		System.out.println(new RecordDaoimpl().todaywords(email));
	}

	@Override
	public Boolean delete() throws Exception {
		String sql;
		int update = 0;

			sql = "delete  from  record  where  not(year = ? and month = ? and day =?) and flag = 0 ";
			PreparedStatement st1 = Dbconnect.init(sql);

			st1.setInt(1, toyear);
			st1.setInt(2, tomonth);
			st1.setInt(3, today);

			update = Dbconnect.update(st1);
			Dbconnect.close(st1);





		return update > 0;
	}

	@Override
	public Boolean update(String email, int worldID, int flag) throws Exception {
		String sql="update record set flag=? where email =? and wordID =?";
		PreparedStatement st = Dbconnect.init(sql);
		st.setInt(1,flag);
		st.setString(2,email);
		st.setInt(3,worldID);
		int update = Dbconnect.update(st);
		Dbconnect.close(st);

		return update>0;
	}
}